package com.qf.service.impl;

import com.qf.dao.UserDao;
import com.qf.dao.impl.UserDaoImpl;
import com.qf.domain.User;
import com.qf.service.UserService;
import com.qf.utils.EmailUtils;
import com.qf.utils.Md5Utils;

public class UserServiceImpl implements UserService {
    UserDao userDao=new UserDaoImpl();
    //注册
    @Override
    public void regist(User user) {
        //判断用户是否存在
        boolean b = userDao.select(user.getUsername());
        if (b){
            throw new  RuntimeException("用户已存在");
        }
        //加密
        String s = Md5Utils.md5(user.getPassword());
        user.setPassword(s);
        userDao.insert(user);
        //发送邮箱
        EmailUtils.sendEmail(user);
    }
    //登陆
    @Override
    public User login(String username, String password) {
        //加密
        String s = Md5Utils.md5(password);
        User user = userDao.select(username, s);
        if (user.getFlag()!=1){
            throw new RuntimeException("账户失效或者未激活");
        }
        return user;
    }

    @Override
    public boolean checkUserName(String username) {
        return userDao.select(username);
    }
}
